//
//  MonthDayView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/24.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol MonthDayViewDelegate {
    
    func retrunDate(date:String)
}

class MonthDayView: UIView {
    
    // 获取数据
    private let infos = AirLineUtil.shareInstance.getOnlyYearMoth()
    // 滑动View
    private lazy var scrollView = UIScrollView()
    
    // MARK: 代理
    public var delegate:MonthDayViewDelegate?
    
    override func layoutSubviews() {
        // 数量
        let columns = 7
        // 宽度
        let btnW = MARGIN45
        // 高度
        let btnH = MARGIN60
        // 间距
        let margin = (frame.width - CGFloat(columns) * btnW) / CGFloat(columns + 1)
        var tempHeight:CGFloat = 0
        // 开始
        for i in 0..<infos.count {
            let time = infos[i]
            // 年月
            let yearmonth = time.yearmonth
            let label = UILabel(title: yearmonth, color: UIColor.black, size: 22)
            label.frame = CGRect(x: MARGIN, y: MARGIN + tempHeight, width: MARGIN140, height: MARGIN20)
            scrollView.addSubview(label)
            // 天数
            for j in 0...time.fakeDays {
                if j < time.seek - 1 {
                    continue
                }
                // 日
                let day = j - (time.seek - 2)
                // 坐标
                let btnX = CGFloat(j % columns) * (margin + btnW) + margin
                let btnY = CGFloat(j / columns) * (margin + btnH) + margin
                // 摆放
                let btn = UIButton()
                btn.tag = i
                btn.layer.cornerRadius = 5
                btn.layer.masksToBounds = true
                btn.setTitleColor(UIColor.black, for: .normal)
                btn.setTitle(String(describing: day), for: .normal)
                btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
                btn.frame = CGRect(x: btnX, y: btnY + label.frame.maxY + MARGIN15, width: btnW, height: btnH)
                scrollView.addSubview(btn)
                //当前日期前置为灰色、且不可点击
                if let currDay = time.currentDay,day < currDay {
                    btn.isEnabled = false
                    btn.setTitleColor(UIColor.gray, for: .normal)
                }
                // 选中当前日
                if let currDay = time.currentDay,day == currDay {
                    btn.backgroundColor = COLOR_RED
                }
                // 处理最后一个数据
                if j == time.fakeDays {
                    // 存储末位BTN的高度
                    tempHeight = btn.frame.maxY + MARGIN
                    // 设置可滑动的范围
                    scrollView.contentSize = CGSize(width: frame.width, height: tempHeight + MARGIN40)
                }
                // 添加点击事件
                btn.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            }
        }
        // 设置 scrollview
        scrollView.showsVerticalScrollIndicator = false
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        addSubview(scrollView)
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        guard let day = btn.titleLabel?.text else {
            return
        }
        // 修改颜色
        UIView.animate(withDuration: 0.3, animations: {
            self.updateBtnColor(btn: btn)
        }) { (isYes) in
            if !isYes {
                return
            }
            // 获取年月
            let info = self.infos[btn.tag].yearmonth
            // 替换字符
            let yearmoth = info.replacingOccurrences(of: "年", with: "-").replacingOccurrences(of: "月", with: "-")
            // 转化日期
            let date = AirLineUtil.shareInstance.getConvertDate(time: yearmoth + day,isYear: true)
            if let callback = self.delegate {
                callback.retrunDate(date: date)
            }
        }
    }
    
    private func updateBtnColor(btn:UIButton) -> Void {
        for item in scrollView.subviews {
            item.backgroundColor = UIColor.white
        }
        btn.backgroundColor = COLOR_RED
    }
}
